﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.UI.YGUI;

public class ColorSliderGroup : MonoBehaviour
{
    //创建一个颜色数组
    Color32[] colors = new Color32[]{
          new Color32(1, 0, 0,1),
          new Color32(1, 1, 0,1),
          new Color32(0, 1, 0,1),
          new Color32(0, 1, 1,1),
          new Color32(0, 0, 1,1),
          new Color32(1, 0, 1,1),
          new Color32(1, 0, 0,1)};

    public YSlider h_slider; //颜色调节器
    public YSlider s_slider; //饱和度调节器
    public YSlider v_slider; //亮度调节器
    public YText h_text;
    public YText s_text;
    public YText v_text;
    public YImage color_img;
    public uint color_hex;
    public int light_hex;
    private float h_value;
    private float s_value;
    private float v_value;
    
    private string tag = "[C#][Character] ColorSliderGroup : ";
    
    //将Color值转为Hex值
    public static uint ToHex(Color c) => ((uint)(c.a * 255) << 24) | ((uint)(c.r * 255) << 16) | ((uint)(c.g * 255) << 8) | (uint)(c.b * 255);
    
    //将Hex值转为Color值
    public static Color ToRGBA(uint hex) => new Color(((hex >> 16) & 0xff) / 255f, ((hex >>  8) & 0xff) / 255f, (hex & 0xff) / 255f, ((hex >> 24) & 0xff) / 255f);
    
    // Start is called before the first frame update
    void Awake()
    {
        //设置值变化事件
        h_slider.onValueChanged.AddListener(OnHSliderValueChanged);
        s_slider.onValueChanged.AddListener(OnSSliderValueChanged);
        v_slider.onValueChanged.AddListener(OnVSliderValueChanged);
        
        var hueTex = new Texture2D(colors.Length, 1);
                hueTex.SetPixels32(colors);
                hueTex.Apply();
                
        //绘制Hue颜色条
        h_slider.transform.Find("Background").GetComponent<YImage>().sprite = 
                Sprite.Create(hueTex, new Rect(Vector2.zero, new Vector2(colors.Length, 1)), Vector2.one * 0.5f);
    }

    void Start()
    {
        UpdateColor();
    }
    
    // Update is called once per frame
    void OnHSliderValueChanged(float _value)
    {
        Debug.Log(string.Format("{0}OnHSliderValueChanged, _value = {1}", tag, _value));
        h_value = _value;
        UpdateColor();
    }

    void OnSSliderValueChanged(float _value)
    {
        Debug.Log(string.Format("{0}OnSSliderValueChanged, _value = {1}", tag, _value));
        s_value = _value/100;
        UpdateColor();
    }
    
    void OnVSliderValueChanged(float _value)
    {
        Debug.Log(string.Format("{0}OnVSliderValueChanged, _value = {1}", tag, _value));
        v_value = _value/100;
        UpdateColor();
    }
    
    public void UpdateColor()
    {
        Color color = Color.HSVToRGB(h_value, s_value, v_value);
        color_hex = ToHex(color);
        Debug.Log(string.Format("{0}UpdateColor: hsv = {1},{2},{3}, color = ({4},{5},{6},{7}), hex = {8}", 
                tag, h_value, s_value, v_value, color.r, color.g, color.b, color.a, color_hex));
        
        if (h_text != null)
            h_text.text = string.Format("#{0}", ColorUtility.ToHtmlStringRGB(color));
        if (s_text != null)
            s_text.text = string.Format("{0}", (int)(s_value * 100));
        if (v_text != null)
            v_text.text = string.Format("{0}", (int)(v_value * 100));
            
        if (color_img !=null)
            color_img.color = color;
    }
    
    public void setInitColor(uint hex)
    {
        color_hex = hex;
        
        float h, s, v;
        Color color = ToRGBA(hex);
        Debug.Log(string.Format("{0}setInitColor = {1}, {2}, {3}, {4}", tag, color.r, color.g, color.b, color.a));
        Color.RGBToHSV(color, out h, out s, out v);
        h_value = h;
        s_value = s;
        v_value = v;
        
        if (h_slider != null) h_slider.value = h;
        if (s_slider != null) s_slider.value = s * 100;
        if (v_slider != null) v_slider.value = v * 100;
        
        UpdateColor();
    }
}
